﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Cashier_Interface;

namespace Cashier.pos
{
    public partial class GoodsNormForm : Form
    {
        private IGoodsNorm iGoodsNorm;
        private DataTable dt;
        private String displayMember;
        private String valueMember;
        private bool isselected = true;

        public GoodsNormForm(IGoodsNorm iGoodsNorm, DataTable dt, String displayMember, String valueMember)
        {
            InitializeComponent();
            this.iGoodsNorm = iGoodsNorm;
            this.dt = dt;
            this.displayMember = displayMember;
            this.valueMember = valueMember;

            this.InitialListBox();
        }

        /// <summary>
        /// 初始化ListBox
        /// </summary>
        private void InitialListBox()
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                listBox1.DisplayMember = this.displayMember;
                listBox1.ValueMember = this.valueMember;
                listBox1.DataSource = dt;
            }
        }

        /// <summary>
        /// 重载键盘事件
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                isselected = false;
                this.Close();
            }
            else if (keyData == Keys.Enter)
            {
                isselected = true;
                if (!button_ok.Focused)
                    this.Close();
            }
            return base.ProcessDialogKey(keyData);
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GoodsNormForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (isselected)
            {
                DataRow row = dt.Rows[listBox1.SelectedIndex];
                iGoodsNorm.SelectGoodsNorm(row);
            }
            else
            {
                iGoodsNorm.SelectGoodsNorm(null);
            }
        }

        /// <summary>
        /// OK事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            isselected = true;
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            isselected = false;
            this.Close();
        }
    }
}
